const app = getApp()
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
const MD5Utils = require('../../utils/MD5.js');

//获取应用实例  

Page({

  data: {
    mark: 'sc',
    detail: {
      items: []
    },
    goods: {

      items: []
    },
    ii: '',
    img: '',
    defaultimg:'',
    storeadress: '',
    xxaddress:'',
    stores_id: '',
    stores_name: '',
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    userInfo: {},



  },



  onLoad: function (options) {

    this.setData({
      stores_id: options.stores_id
    })
    this.detailDemo();
    //通过home页面点击事件获取的数据
    this.detailgoods();



  },

  detailgoods: function (e) {
    var that = this;



    let paramsData = {
      store_id: this.data.stores_id,
    }

    console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + this.data.stores_id)
    console.log(this.data.store_id)
    console.log(JSON.stringify(paramsData))

    const data = {
      //店铺精品特卖详情
      service: 'goods.recommendItemStoreForConsumerListPageFind',
      //params: '{"store_id":"' + this.data.stores_id + '"}',
      params: JSON.stringify(paramsData),
      url_type: 'goods',
      app_token: app.globalData.app_token,
      security_code: app.globalData.security_code
    }
    const goods = this.data.goods
    console.log(data)


    api.requestData({
      data,
      success: (res) => {
        var resData = res.data;
        goods.items = resData.data.list;
        console.log(goods.items)

        var docList = res.data.data.doc_url
        var doc_url_key = Object.keys(res.data.data.doc_url)
        var itemList = this.data.goods.items
        for (var i = 0; i < itemList.length; i++) {
          var item_pic_info_str = itemList[i].image_info;

          if (item_pic_info_str != '') {
            for (var e = 0; e < doc_url_key.length; e++) {
              if (item_pic_info_str === doc_url_key[e]) {
                itemList[i].pic_info = docList[doc_url_key[e]]
              }
            }
          } else {
            itemList[i].pic_info = ''
          }
          itemList[i].pic_info = itemList[i].pic_info.replace("https", "http")
        
        }

        util.getDistance(res.data.data.doc_url)

        
        this.setData({
          goods: goods,
          stores_name: this.data.detail.items.stores_name
        })
        

      },
      fail: (res) => {
        console.log("fail 返回 res.data>>>" + res.data);
      },
      complete: (res) => {

      }
    })


  },




  detailDemo: function (e) {

    var that = this;
    var member_id = app.globalData.memberInfo.member_id



    const data = {
      //店铺详情接口
      service: 'member.storeDetailForConsumerFind',
      params: '{"consumer_id":"' + member_id + '","stores_id":"' + this.data.stores_id + '"}',
      url_type: 'member',

      app_token: app.globalData.app_token,
      security_code: app.globalData.security_code

    }

    const detail = this.data.detail
    console.log(data)
    api.requestData({
      data,
      success: (res) => {
        console.log("success 返回 res.data>>>" + res.data);
        var resData = res.data;
     
        detail.items = resData.data.detail;
        console.log("detail>>>" + detail);
        var xxlist = resData.data.detail.stores_thumbnail_path;
        var storeimg = new Array();
        var firstimg = new Array();
        firstimg.push(resData.data.detail.neighbor_pic_path);
        if (xxlist == null || xxlist == "") {
          storeimg = ""
        } else {
          var xxxList = JSON.parse(xxlist)
          for (var e = 0; e < xxxList.length; e++) {

            if (xxxList[e].path_id == null || xxxList[e].path_id == "") {

            } else {
              storeimg.push(xxxList[e].path_id)
            }
          }
        }

        if (storeimg == "" || storeimg == null) {
          var slideshow = resData.data.detail.neighbor_pic_path
          var docList = res.data.data.doc_url
          var itemList = docList
          var imgList = new Array();
          imgList.push(docList[slideshow])
        }
        else {
          var slideshow = firstimg.concat(storeimg);
          var docList = res.data.data.doc_url
          var itemList = docList
          var imgList = new Array();

          for (var e = 0; e < slideshow.length; e++) {
            slideshow[e] = docList[slideshow[e]]
            imgList.push(slideshow[e])

          }
          
          
        }
        var  defaultimg = new Array();
        defaultimg.push('http://oss-img.meitianhui.com/test/communityDocs/201801/41fdf4389d19478c9d40ccd482037937.jpg')
       
        var eadress = this.data.detail.items.area_desc.replace(/,/g, "")
        if (this.data.detail.items.address.indexOf("/") != -1) {
          var i = this.data.detail.items.address.split("/")
          var a = i.length
          var xxaddress = i[a - 1]
       
        } else {
          var xxaddress = this.data.detail.items.address
        }

        if (imgList != '') {
          for (var e = 0; e < imgList.length; e++) {
            imgList[e] = imgList[e].replace("https", "http")

          }}

       
        

        this.setData({
          xxaddress: xxaddress,
          detail: detail,
          stores_name: this.data.detail.items.stores_name,
          img: imgList,
          defaultimg :defaultimg,
          storeadress: eadress
        })

        wx.setNavigationBarTitle({
          title: this.data.stores_name//页面标题为路由参数
        })
      },
      fail: (res) => {
        console.log("fail 返回 res.data>>>" + res.data);
      },
      complete: (res) => {

      }
    })


  },
  //门店收藏
  sc: function (e) {
    var that = this;

    if (app.globalData.hasLogin == false){

      wx.navigateTo({
        url: '/pages/login/login',
      })

    }else{

      var member_id = app.globalData.memberInfo.member_id
    
      const data = {
        //门店收藏接口
        service: 'stores.consumer.favoriteStore',
        params: '{"consumer_id":"' + member_id + '","stores_id":"' + this.data.stores_id + '"}',
        url_type: 'member',

        app_token: app.globalData.app_token,
        security_code: app.globalData.security_code

      }
     

      const detail = this.data.detail
      console.log(data)
      console.log("##############################################################")
      console.log(this.data.stores_id)
      this.setData({
        mark: 'qxsc',

      })

      api.reqData({
        data,
        success: (res) => {
          wx.showToast({
            title: '收藏成功',
            icon: 'succes',
            duration: 1000,
            mask: true,

          })
        },
      })


    }



  },



  shop: function (e) {
/*     wx.showToast({
      title: '暂未开发',
      icon: 'fail',
      duration: 1000,
      mask: true
    }) */

    wx.showModal({
      title: '温馨提示',
      content: '暂无此功能',
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else {
          console.log('用户点击取消')
        }
      }
    })

  },



  //取消收藏
  qxsc: function (e) {
    var that = this;
    var member_id = app.globalData.memberInfo.member_id
    const data = {
      //取消收藏门店接口
      service: 'stores.consumer.favoriteStoreCancel',
      params: '{"consumer_id":"' + member_id + '","stores_id":"' + this.data.stores_id + '"}',
      url_type: 'member',
      app_token: app.globalData.app_token,
      security_code: app.globalData.security_code

    }
    console.log("********************************>>>" + this.data.stores_id);

    const detail = this.data.detail
    console.log(data)

    this.setData({
      mark: 'sc',

    })

    api.reqData({
      data,
      success: (res) => {
        wx.showToast({
          title: '已取消收藏',
          icon: 'succes',
          duration: 1000,
          mask: true,

        })
      },
    })
  },
  //模拟拨打电话
  calling: function () {
    wx.makePhoneCall({
      phoneNumber: this.data.detail.items.contact_tel, //此号码并非真实电话号码，仅用于测试
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },


  orderlist: function () {
    wx.switchTab({
      url: '../buy/orderlist/orderlist'
    })
  },

})  